package com.gimi.cloud.bbp.app.consumer.controller;

import com.gimi.cloud.bbp.app.consumer.service.RegisterService;
import com.gimi.cloud.bbp.app.consumer.service.SysConfigService;
import com.gimi.cloud.bbp.app.consumer.service.UserService;
import com.gimi.cloud.bbp.app.consumer.service.home.HomePageService;
import com.gimi.cloud.bbp.app.consumer.service.mall.MallCartService;
import com.gimi.cloud.bbp.app.consumer.service.mall.MallMessageService;
import com.gimi.cloud.bbp.app.consumer.service.mall.MallSuggestService;
import com.gimi.cloud.bbp.app.consumer.service.moreInfo.MoreInfoService;
import com.gimi.cloud.bbp.app.consumer.service.order.OrderService;
import com.gimi.cloud.bbp.app.consumer.service.product.CategoryService;
import com.gimi.cloud.bbp.app.consumer.service.product.ProductService;
import com.gimi.cloud.bbp.app.consumer.service.settlement.SettlementService;
import com.gimi.cloud.bbp.app.consumer.service.sys.PublicService;
import com.gimi.cloud.bbp.app.consumer.service.user.UserExpressService;
import com.gimi.cloud.bbp.app.consumer.service.user.UserInfoService;
import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.enums.ServiceNameEnum;
import com.gimi.cloud.bbp.common.utils.CommonUtil;
import com.gimi.cloud.bbp.common.utils.JsonUtil;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.common.utils.reqhead.Util;
import com.gimi.cloud.bbp.dto.base.BaseDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by luck on 2017/09/28.
 */
@RestController
public class JiMiMicroServiceConsumerController {

	private static Logger logger = LoggerFactory.getLogger(JiMiMicroServiceConsumerController.class);
	private final HomePageService homePageService;
	private final RegisterService registerService;
	private final UserService userService;
	private final UserInfoService userInfoService;
	private final UserExpressService userExpressService;
	private final MoreInfoService moreInfoService;
	private final OrderService orderService;
	private final ProductService productService;
	private final CategoryService categoryService;
	private final MallSuggestService mallSuggestService;
	private final MallCartService mallCartService;
	private final SettlementService settlementService;
	private final MallMessageService mallMessageService;
	private final SysConfigService sysConfigService;
	private final PublicService publicService;

	@Autowired
	public JiMiMicroServiceConsumerController(HomePageService homePageService, RegisterService registerService, UserService userService, UserInfoService userInfoService, UserExpressService userExpressService, MoreInfoService moreInfoService, OrderService orderService, ProductService productService, CategoryService categoryService, MallSuggestService mallSuggestService, MallCartService mallCartService, SettlementService settlementService, MallMessageService mallMessageService, SysConfigService sysConfigService, PublicService publicService) {
		this.homePageService = homePageService;
		this.registerService = registerService;
		this.userService = userService;
		this.userInfoService = userInfoService;
		this.userExpressService = userExpressService;
		this.moreInfoService = moreInfoService;
		this.orderService = orderService;
		this.productService = productService;
		this.categoryService = categoryService;
		this.mallSuggestService = mallSuggestService;
		this.mallCartService = mallCartService;
		this.settlementService = settlementService;
		this.mallMessageService = mallMessageService;
		this.sysConfigService = sysConfigService;
		this.publicService = publicService;
	}

	/**
	 * 作为user服务的统一入口.
	 *
	 * @param requestData
	 *            请求参数
	 * @param request
	 *            httpRequest
	 * @param response
	 *            response
	 */
	@RequestMapping(value = "service")
	public void userService(@RequestBody(required = false) String requestData, @RequestParam(value = "file",required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("===>进入user微服务主程序入口.");
		logger.info("token===>" + request.getHeader("authorization"));
		logger.info("serviceName===>" + request.getHeader("serviceName"));
		logger.info("userId===>" + request.getHeader("userId"));

		Response resp = new Response();

		/**
		 * 讲网关中的添加人更新人属性放入body
		 */
		Map<String,String> headerMaps = new HashMap<>();
		//设置请求body的key为authToken，对应头的key为authorization
		headerMaps.put("authToken","authorization");
		String[] headersKeys = new String[]{"ip","userId","_userId","serviceName"};
		requestData = CommonUtil.initHeaderInfoToBody(request,requestData,headersKeys,headerMaps,false);
		ServiceNameEnum serviceNameEnum = ServiceNameEnum.getStatus(request.getHeader("serviceName"));
		switch (serviceNameEnum) {
			case APP_HOME_GETINDEX: //获取首页
				homePageService.getIndex(requestData, request, response);
				break;
			case APP_SPECIAL_GETLIST: //获取专场
				homePageService.getSpecialList(requestData, request, response);
				break;
			case APP_HOME_PAGE: //获取APP指定页面(首页，专题，活动等)
				homePageService.getPage(requestData,request,response);
				break;
			case SYS_CONFIG_GET_VALUE:
				sysConfigService.getConfigValueByCfKey(requestData, request, response);
				break;
			case APP_MESSAGE_GET_LIST_BY_RECEIVERUSER: //获取消息列表
				mallMessageService.getMessageListByReceiveUser(requestData,request,response);
				break;
			case APP_MESSAGE_USER_DEL_BY_ID: //用户删除消息
				mallMessageService.userDelMessageByMeId(requestData,request,response);
				break;
			case APP_MESSAGE_GET_BY_ID: //根据ID获取消息详情
				mallMessageService.getMessageByMeId(requestData,request,response);
				break;
			case APP_MESSAGE_GET_BY_USERID_AND_STATUS: //获取用户未读消息条数
				mallMessageService.getMessageByUserIdAndStatus(requestData,request,response);
				break;
			case APP_HOME_PAGE_COLUMNS:
				homePageService.columnList(requestData,request,response);
				break;
			case APP_HOME_PAGE_ADS:
				homePageService.adList(requestData,request,response);
				break;
			/**app对会员详细信息 userInfo的操作*/
			case USERINFO_ADD://添加用户详细信息
				userInfoService.addUserInfo(requestData,request,response);
				break;
			case USERINFO_UPDATE://用户详细信息修改
				userInfoService.updateUserInfo(requestData, request, response);
				break;
			case PHOTO_FILE_UPLOAD://详细信息中头像图片上传
				userInfoService.photoFileUpload(requestData,response,request);
				break;
			/**app对会员详细配送地址的操作*/
			case USEREXPRESS_ADD://添加收获地址
				userExpressService.addExpressInfo(requestData,request,response);
				break;
			case USEREXPRESS_SINGLE://获取单个收获地址
				userExpressService.getExpressInfoSingle(requestData, request, response);
				break;
			case USEREXPRESS_UPDATE://修改收货地址
				userExpressService.updateExpressInfo(requestData, request, response);
				break;
			case USEREXPRESS_PAGELIST://收获地址列表
				userExpressService.expressPageListByUser(requestData, request, response);
				break;
			case USEREXPRESS_DELETE://删除收获地址
				userExpressService.expressDelete(requestData, request, response);
				break;
			case USER_GET_REGISTERCODE:// 获取登录验证码
				userService.getUserRegisterCode(requestData, response, request);
				break;
			case USER_DOLOGIN_BY_PWDCODE:// 验证码验证
				registerService.doLoginByPwdCode(requestData, response, request);
				break;
			case USER_DOLOGIN_BY_PASSWORD:// 用户登录(密码登录)
				registerService.doLoginByPassword(requestData, response, request);
				break;
			case INIT_PASSWORD://初始化密码
				registerService.initPwd(requestData, response, request);
				break;
			case MODIFY_PASSWORD://修改密码
				registerService.modifyPwd(requestData, response, request);
				break;
			case GET_MODIFYPASSWORD_CODE:// 获取修改密码验证码
				userService.getModifypasswordCode(requestData, response, request);
				break;
			case USER_CHECK_MODIFYPASSWORD:// 修改密码验证码验证
				registerService.checkModifypassword(requestData, response, request);
				break;
			/**订单模块end*/

			case PEODUCT_DETAIL://商品详情:基本信息展示
				productService.showProductDetail(requestData, response, request);
				break;
			case PEODUCT_PROP://商品详情:所有规格列表
				productService.getProductProp(requestData, response, request);
				break;
			case SKU_MODEL_VALUE://商品详情:有库存的sku列表
				productService.getModelValueList(requestData, response, request);
				break;
			case PRODUCTS_OF_GRADE://获取某一级或二级分类下的所有商品
				productService.getProductsOfGrade(requestData, response, request);
				break;
			case SEARCH_PRODUCTS://获取二级分类下的所有商品
				productService.searchProducts(requestData, response, request);
				break;
				
			/**********************************************以下为身份认证接口***********************************************/
			case UPLOAD_CARD_PHOTO://上传身份证等图片,其中front为身份证正面，back为身份证背面，avatar为身份证小头像，hand为人脸识别的照片
				userInfoService.uploadCardPhoto(response,request);
				break;
			case UPDATE_USERIDINFO://更新身份证信息
				userInfoService.updateUserIdInfo(requestData,response , request);
				break;
			case UPDATE_UNIMPORTANTUSERINFO://更新身份证信息
				userInfoService.updateUnimportantUserinfo(requestData,response , request);
				break;
			case GET_USERIDINFO://用户详情查询
				userInfoService.getUserInfo(requestData, response, request);
				break;
			case UPDATE_USERBANKCARD://更新身份证信息
				userInfoService.updateUserbankcard(requestData,response , request);
				break;
			case BANKCARDSIGN: //连连支付签约绑卡前的字符串拼接
				userInfoService.bankCardSign(requestData, response, request);
				break;
			case GETUSERBANKCARD: // 银行卡查询接口
				userInfoService.getUserBankcard(requestData, response, request);
				break;
			case GETAGREEMENT://获取协议信息
				userInfoService.getAgreement(request,response,requestData);
				break;
			/**********************************************************************************************************/


			/**********************************************以下为提速接口***********************************************/
			case GET_ZM_INFO://获取芝麻信用评分
				moreInfoService.getZmInfo(requestData, response, request);
				break;

			case TAO_BAO_LOGIN://淘宝-登录接口
				moreInfoService.taobaoLogin(requestData, response, request);
				break;

			case GET_SALARY_CARD_INFO://获取工资卡信息
				moreInfoService.getSalaryCardInfo(requestData, response, request);
				break;

			case GET_SUPPORTED_CREDITCARD_BANKS://获取蜂巢信用卡支持银行列表
				moreInfoService.getSupportedCreditcardBanks(requestData, response, request);
				break;
			case GET_CREDITCARD_LOGIN_PARAMETER://获取蜂巢信用卡银行登陆参数
				moreInfoService.getCreditcardLoginParameter(requestData, response, request);
				break;
			case SUB_CREDITCARD_BASIC_PARAM://提交蜂巢信用卡基本登录参数
				moreInfoService.subCreditcardBasicParam(requestData, response, request);
				break;

			case GET_FENGCHAO_EMAIL_LIST://获取蜂巢信用卡支持的邮箱列表
				moreInfoService.getFengchaoEmailList(requestData, response, request);
				break;
			case GET_FENGCHAO_POSITION_LIST://蜂巢邮箱支持的Position列表
				moreInfoService.getFengchaoPositionList(requestData, response, request);
				break;

			case GET_PROVIDENTFUND_INFO_SUPPORTED_CITY_LIST://获取公积金城市登录列表
				moreInfoService.getProvidentFundInfoSupportedCityList(requestData, response, request);
				break;
			case GET_PROVIDENTFUND_INFO_CITY_LOGIN_PARAMETER://获取公积金地区登录参数
				moreInfoService.getProvidentFundInfoCityLoginParameter(requestData, response, request);
				break;
			case GET_PROVIDENTFUND_INFO_IMAGE://获取公积金验证码图片
				moreInfoService.getProvidentFundInfoImage(requestData, response, request);
				break;
			case GETP_ROVIDENT_FUND_INFO://登录并且获取公积金信息
				moreInfoService.getProvidentFundInfo(requestData, response, request);
				break;

			case GET_SHEBAO_CITY_LIST://获取社保城市列表
				moreInfoService.getSheBaoCityList(requestData, response, request);
				break;
			case GET_SHEBAO_CITY_LOGIN_PARAMER://获取社保登录参数
				moreInfoService.getSheBaoCityLoginParamer(requestData, response, request);
				break;
			case GET_SHEBAO_CODE_IMAGE://获取社保验证码图片
				moreInfoService.getSheBaoCodeImage(requestData, response, request);
				break;
			case GET_SHEBAO_LOGIN_AND_GRAB://登录并抓取社保账号
				moreInfoService.getSheBaoLoginAndGrab(requestData, response, request);
				break;
			case CATEGORY_FIRST_GRADE_LIST://商品品类列表(获取所有一级分类及一级下的所有分类)
				categoryService.firstGradeCategories(request,response);
				break;
			case CATEGORY_SECOND_GRADE_LIST://商品品类列表(查询所有二级级分类)
				categoryService.secondGradeCategories(requestData,request,response);
				break;
			case SUBMIT_MALL_SUGGEST: //提交建议
				mallSuggestService.submitMallSuggest(requestData,request,response);
				break;
			case ADD_GOODS_TO_SHOPPING_CART: //添加商品到购物车
				mallCartService.addGoodsToShoppingCart(requestData,request,response);
				break;
			case SHOW_SHOPPING_CART://查询购物车
				mallCartService.showShoppingCart(requestData,request,response);
				break;
			case UPDATE_SHOPPING_CART://更新购物车中的单个商品
				mallCartService.updateShoppingCart(requestData,request,response);
				break;
			case DELETE_SHOPPING_CART://批量删除购物车中的商品
				mallCartService.deleteShoppingCart(requestData,request,response);
				break;
			case GENERATING_CLEARING_INFORMATION://生成结算信息
				settlementService.generatingClearingInformation(requestData,request,response);
				break;
			case GENERATING_ORDER://生成订单
				settlementService.generatingOrder(requestData, request, response);
				break;
			case USER_ORDER_LIST://用户订单列表
				orderService.userOrderList(requestData,request,response);
				break;
			case MODIFY_ORDER_STATUS://修改用户订单状态
				orderService.modifyOrderStatus(requestData,request,response);
				break;
			case GET_ORDER_DETAILS://获取单个订单详情
				orderService.getOrderDetails(requestData,request,response);
				break;
			case SUBMISSION_OF_PAYMENT_STATUS://提交订单支付状态
				settlementService.submissionOfPaymentStatus(requestData,request,response);
				break;
			case GET_PAYMENT_PARAMETERS://获取订单支付参数
				settlementService.getPaymentParameters(requestData,request,response);
				break;
			case BUY_DIRECT://直接购买
				settlementService.buyDirect(requestData,request,response);
				break;
			case GET_ORDER_PAY_AMOUNT://获取订单支付金额
				settlementService.getOrderPayAmount(requestData,request,response);
				break;
			case GET_HOME_PAGE_DATA://获取首页数据 //TODO 开发未完成
				homePageService.getHomePageData(requestData,request,response);
				break;
			case GET_HOME_PAGE_CAROUSEL_FIGURE://获取首页轮播图
				homePageService.getHomePageCarouselFigure(requestData,request,response);
				break;
			//文件上传
			case PUBLIC_FILE_UPLOAD:
				publicService.fileUpload(request,response,JsonUtil.jsonToBean(requestData, BaseDTO.class));
				break;
			/**********************************************************************************************************/
			default:
				resp.setResponseCode(ResponseCodeEnum.SERVICE_NAME_NOT_FOUND);
				Util.sendResponse(JsonUtil.toJson(resp), response);
				break;
		}
		logger.info("===>执行完毕.\n");
	}

}
